package com.animee.day11.utils;

import com.animee.day11.ResourceTable;
import com.animee.day11.bean.FilmInfoBean;
import com.bumptech.glide.Glide;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class FilmGridProvider extends BaseItemProvider{
    AbilitySlice context;
    List<FilmInfoBean> mDatas;
    public FilmGridProvider(AbilitySlice context, List<FilmInfoBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }
    @Override
    public int getCount() {
        return mDatas.size();
    }
    @Override
    public Object getItem(int i) {return mDatas.get(i);}
    @Override
    public long getItemId(int i) {return i;}

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        if (component == null){
            component = LayoutScatter.getInstance(context)
                    .parse(ResourceTable.Layout_item_film_grid
                            ,componentContainer,false);
        }
        Image iv = component.findComponentById(ResourceTable.Id_item_grid_iv);
        FilmInfoBean bean = mDatas.get(i);

        String coverUrl = bean.getCover_url();
        if (coverUrl==null||coverUrl.isEmpty()){
            iv.setPixelMap(ResourceTable.Media_load_failed);
        }else{
            Glide.with(context).load(coverUrl)
                    .placeholder(ResourceTable.Media_load_failed)
                    .error(ResourceTable.Media_load_failed)
                    .into(iv);
        }
        return component;
    }
}